//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using OpenLS.Core.Serialization;

namespace OpenLS.Spreadsheet.Data
{
    

    
    public class TextConnection : DataConnection
    {
        public TextConnection()
        {

        }
        public TextConnection(string sourceFile)
        {
            this._sourceFile = sourceFile;
        }

        internal override void ReadProperties(ReadContext context)
        {

            using (ReadContext c = context.Read("textPr"))
            {
                Prompt = c.GetOptionalBoolean("prompt", true);
                FileType = c.GetOptionalEnum<TextFileType>("fileType", TextFileType.Windows);
                CodePage = c.GetOptionalUIntegerAsInteger("codePage", 1252);
                FirstRow = c.GetOptionalUIntegerAsInteger("firstRow", 1);
                SourceFile = c.GetOptionalString("sourceFile");
                IsDelimited = c.GetOptionalBoolean("delimited", true);
                Decimal = c.GetOptionalString("decimal", ".");
                Thousands = c.GetOptionalString("thousands", ",");
                Tab = c.GetOptionalBoolean("tab", true);
                Space = c.GetOptionalBoolean("space", false);
                Comma = c.GetOptionalBoolean("comma", false);
                SemiColon = c.GetOptionalBoolean("semiColon", false);
                Consecutive = c.GetOptionalBoolean("consecutive", false);
                Qualifier = c.GetOptionalEnum<Qualifier>("qualifier", Qualifier.DoubleQuote);
                Delimiter = c.GetOptionalString("delimiter");
                Fields.Clear();
                foreach (TextConnectionField f in c.ReadOptionalListElement<TextConnectionField>("textFields", "textField"))
                {
                    Fields.Add(f);
                }
            }
        }

        internal override void WriteProperties(WriteContext context)
        {
            using (WriteContext c = context.Write("textPr"))
            {
                c.SetOptionalBoolean("prompt", Prompt, true);
                c.SetOptionalEnum<TextFileType>("fileType", FileType, TextFileType.Windows);
                c.SetOptionalUIntegerAsInteger("codePage", CodePage, 1252);
                c.SetOptionalUIntegerAsInteger("firstRow", FirstRow, 1);
                c.SetOptionalString("sourceFile", SourceFile);
                c.SetOptionalBoolean("delimited", IsDelimited, true);
                if (Decimal != null)
                    c.SetOptionalString("decimal", Decimal, ".");
                if (Thousands != null)
                    c.SetOptionalString("thousands", Thousands, ",");
                c.SetOptionalBoolean("tab", Tab, true);
                c.SetOptionalBoolean("space", Space, false);
                c.SetOptionalBoolean("comma", Comma, false);
                c.SetOptionalBoolean("semiColon", SemiColon, false);
                c.SetOptionalBoolean("consecutive", Consecutive, false);
                c.SetOptionalEnum<Qualifier>("qualifier", Qualifier, Qualifier.DoubleQuote);
                c.SetOptionalString("delimiter", Delimiter);
                if (Fields.Count > 0)
                {
                    using (WriteContext c2 = c.Write("textFields"))
                    {
                        foreach (TextConnectionField f in Fields)
                            c2.WriteElement(f);
                    }
                }
            }
        }

        private List<TextConnectionField> _textFields = GetDefaultFields();
        static List<TextConnectionField> GetDefaultFields()
        {
            List<TextConnectionField> result = new List<TextConnectionField>();
            result.Add(new TextConnectionField());
            return result;
        }

        public List<TextConnectionField> Fields
        {
            get { return _textFields; }
            set { _textFields = value; }
        }
        private bool _prompt;

        public bool Prompt
        {
            get { return _prompt; }
            set { _prompt = value; }
        }
        private TextFileType _fileType;

        public TextFileType FileType
        {
            get { return _fileType; }
            set { _fileType = value; }
        }
        private int codePage = 1252;

        public int CodePage
        {
            get { return codePage; }
            set { codePage = value; }
        }
        private int _firstRow = 1;

        public int FirstRow
        {
            get { return _firstRow; }
            set { _firstRow = value; }
        }
        private string _sourceFile = "";

        public string SourceFile
        {
            get { return _sourceFile; }
            set { _sourceFile = value; }
        }
        private bool _delimited;

        public bool IsDelimited
        {
            get { return _delimited; }
            set { _delimited = value; }
        }
        private string _decimal;

        public string Decimal
        {
            get { return _decimal; }
            set { _decimal = value; }
        }
        private string _thousands;

        public string Thousands
        {
            get { return _thousands; }
            set { _thousands = value; }
        }
        private bool _tab;

        public bool Tab
        {
            get { return _tab; }
            set { _tab = value; }
        }
        private bool _space;

        public bool Space
        {
            get { return _space; }
            set { _space = value; }
        }
        private bool _comma;

        public bool Comma
        {
            get { return _comma; }
            set { _comma = value; }
        }
        private bool _semiColon;

        public bool SemiColon
        {
            get { return _semiColon; }
            set { _semiColon = value; }
        }
        private bool _consecutive;

        public bool Consecutive
        {
            get { return _consecutive; }
            set { _consecutive = value; }
        }
        private Qualifier _qualifier;

        public Qualifier Qualifier
        {
            get { return _qualifier; }
            set { _qualifier = value; }
        }
        private string _delimiter;

        public string Delimiter
        {
            get { return _delimiter; }
            set { _delimiter = value; }
        }
        internal override void SetTypeIfNecessary()
        {
            Type = DataConnectionType.Text;
        }

        internal override void Initialize()
        {
            Type = DataConnectionType.Text;
        }




    }
}
